package com.yl.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yl.dto.GoodsDTO;
import com.yl.model.Goods;
import com.yl.service.IGoodsService;
@ResponseBody
@Controller
public class GoodsController {

	@Resource
	private IGoodsService goodsService;
	
	//后台显示所有商品
	@RequestMapping("/findAllgoods.do")
	public Map findAll(GoodsDTO goodsdto) {
		return goodsService.findAll(goodsdto);
	}	
	//后台更换图片
	@RequestMapping("/picturegoods.do")
	public int picture(int goods_id,String picture) {
		return goodsService.picture(goods_id, picture);
	}	
	//后台上传图片
	@RequestMapping("/uploadpicture.do")
	public Map upload(MultipartFile file) {
		//获取文件原始名
		String name = file.getOriginalFilename();
		System.out.println(name);
		String path = "D:\\upload\\"+name;
		try {
			file.transferTo(new File(path));
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data",name);
		return map;
	}
	//推荐商品
	@RequestMapping("/recommendgoods.do")
	public int recommend(Goods good) {
		return goodsService.recommend(good);
	}		
	//更多推荐
		@RequestMapping("/findAllrecommendgoods.do")
		public Map findAllrecommend() {
			return goodsService.findAllrecommend();
		}	
		
	//后台上架下架
	@RequestMapping("/salegoods.do")
	public int sale(Goods goods) {
		return goodsService.sale(goods);
	}	
	//后台新增商品
	@RequestMapping("/addgoods.do")
	public int add(Goods goods) {
		return goodsService.add(goods);
	}	
	//后台根据id删除商品
	@RequestMapping("/delgoods.do")
	public int del(int goods_id){
		return goodsService.del(goods_id);
	}	
	//后台更新商品信息
	@RequestMapping("/editgoods.do")
	public int edit(Goods goods){
		return goodsService.edit(goods);
	}
	
	
	
}
